package com.wwc.bwcj.util

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.View
import android.widget.PopupMenu
import android.widget.TextView
import com.google.android.material.button.MaterialButton
import com.wwc.bwcj.R

/**
 * 用户选择商品规格（如杯型、冰量、糖量）对话框
 */
@SuppressLint("MissingInflatedId")
class SelectSpecDialog(private val context: Context) : Dialog(context) {
    private lateinit var tvSize: TextView
    private lateinit var tvIce: TextView
    private lateinit var tvSugar: TextView
    private lateinit var btnConfirm: MaterialButton
    private lateinit var btnCancel: MaterialButton

    // 默认规格
    private var size = "大杯"
    private var ice = "全冰"
    private var sugar = "全糖"

    // 初始化
    init {
        setContentView(R.layout.dialog_select_spec)
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        setCanceledOnTouchOutside(false)

        tvSize = findViewById<TextView>(R.id.tv_size)
        tvIce = findViewById<TextView>(R.id.tv_ice)
        tvSugar = findViewById<TextView>(R.id.tv_sugar)
        btnConfirm = findViewById(R.id.btn_confirm)
        btnCancel = findViewById(R.id.btn_cancel)

        // 设置点击事件
        tvSize.setOnClickListener { showDropdown("size") }
        tvIce.setOnClickListener { showDropdown("ice") }
        tvSugar.setOnClickListener { showDropdown("sugar") }

        btnConfirm.setOnClickListener {
            dismiss()
            onConfirmListener?.invoke(size, ice, sugar)
        }

        btnCancel.setOnClickListener {
            dismiss()
        }
    }

    // 显示下拉菜单
    private fun showDropdown(type: String) {
        val items = when (type) {
            "size" -> arrayOf("大杯", "中杯")
            "ice" -> arrayOf("全冰", "去冰", "少冰")
            "sugar" -> arrayOf("全糖", "半糖", "无糖")
            else -> emptyArray()
        }

        val popupMenu = PopupMenu(
            context, findViewById<View>(
                when (type) {
                    "size" -> R.id.ll_size
                    "ice" -> R.id.ll_ice
                    "sugar" -> R.id.ll_sugar
                    else -> R.id.ll_size
                }
            )
        )

        items.forEach { item ->
            popupMenu.menu.add(item)
        }

        popupMenu.setOnMenuItemClickListener { item ->
            when (type) {
                "size" -> size = item.title.toString()
                "ice" -> ice = item.title.toString()
                "sugar" -> sugar = item.title.toString()
            }
            updateText(type)
            true
        }

        popupMenu.show()
    }

    // 根据类型更新对应 TextView 的显示内容
    private fun updateText(type: String) {
        when (type) {
            "size" -> tvSize.text = size
            "ice" -> tvIce.text = ice
            "sugar" -> tvSugar.text = sugar
        }
    }

    // 设置确认按钮的点击监听器
    var onConfirmListener: ((String, String, String) -> Unit)? = null
}